package DataBase;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class AssignmentManager {
    public static DbcpBean dbcp ;
    static{
    	dbcp = new DbcpBean();
    }
	public static int insert(Assignment assignment)
	{
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		int result = 0;
		if(conn!=null)
		{
			Statement stmt;
			try {
				stmt = conn.createStatement();
				String sql = "insert into tb_assignment (ds_ID,fu_ID," +
				"asmt_time,asmt_frequency,asmt_state,asmt_path,asmt_name," +
				"asmt_ftpname,asmt_ftppwd,asmt_next)values('"
		        +assignment.getDs_ID()+"' , '"
		        +assignment.getFu_ID()+"' , '"
		        +assignment.getAsmt_time()+"' , '"
		        +assignment.getAsmt_frequency()+"' , '"
		        +assignment.getAsmt_state()+"' , '"
		        +assignment.getAsmt_path()+"' , '"
		        +assignment.getAsmt_name()+"' , '"
		        +assignment.getAsmt_ftpname()+"' , '"
		        +assignment.getAsmt_ftppwd()+"' , '"
		        +assignment.getAsmt_next()+"');"; 
		        result  = stmt.executeUpdate(sql);
			} catch (SQLException e) {
				System.out.println("AssignmentManager：插入失败");
				e.printStackTrace();
			}
			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	
	
	
	
	
	
	
	
	public static Assignment GetLatestAsmt(){
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();	
		Assignment asmt = null;
		ResultSet result = null;
		try {
			Statement stmt = conn.createStatement();
			String sql = null;
			sql = "select * from tb_assignment where asmt_state = '开启' order by asmt_next,asmt_time";
			result = stmt.executeQuery(sql);
			if(result.next()){
				asmt = new Assignment();
				asmt.setAsmt_frequency(result.getInt("asmt_frequency"));
				asmt.setAsmt_ftpname(result.getString("asmt_ftpname"));
				asmt.setAsmt_ftppwd(result.getString("asmt_ftppwd"));
				asmt.setAsmt_ID(result.getInt("asmt_ID"));
				asmt.setAsmt_name(result.getString("asmt_name"));
				asmt.setAsmt_next(result.getString("asmt_next"));
				asmt.setAsmt_path(result.getString("asmt_path"));
				asmt.setAsmt_state(result.getString("asmt_state"));
				asmt.setAsmt_time(result.getString("asmt_time"));
				asmt.setDs_ID(result.getInt("Ds_ID"));
				asmt.setFu_ID(result.getInt("Fu_ID"));
			}			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				conn.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return asmt;
	}
	
	

	public static int update(Assignment asmt) {
//		DbcpBean dbcp = new DbcpBean();
		Connection conn = dbcp.getConnection();
		int result = 0;
		String sql = null;
		if(conn != null){
			try{
				Statement stmt = conn.createStatement();
				sql = "update tb_Assignment set asmt_name = '" + asmt.getAsmt_name() + "' , " 
				                         +  " asmt_ftpname  = '" + asmt.getAsmt_ftpname()  + "' , " 
				                         +  " asmt_ftppwd = '" + asmt.getAsmt_ftppwd() + "' , " 
				                         +  " asmt_frequency = '" + asmt.getAsmt_frequency() + "' , " 
				                         +  " asmt_next = '" + asmt.getAsmt_next() + "' , " 
				                         +  " asmt_path = '" + asmt.getAsmt_path() + "' , " 
				                         +  " ds_ID= '" + asmt.getDs_ID() + "' , " 
				                         +  " fu_ID= '" + asmt.getFu_ID() + "' , " 
				                         +  " asmt_state = '" + asmt.getAsmt_state() + "'"  
				                         +  " where " +"asmt_ID = " + asmt.getAsmt_ID() + " ; "; 
				result =  stmt.executeUpdate(sql);
			}catch (SQLException e) {
				// TODO Auto-generated catch block
				try {
					conn.close();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				e.printStackTrace();
			}			
		}
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    return result;
	}
	
	public static void main(String[] args) {
//		String my = "陈烨魁";
//        Assignment assignment = new Assignment();
//        assignment.setDs_ID(1);
//        assignment.setFu_ID(1);
//        assignment.setAsmt_time("16:24");
//        assignment.setAsmt_state("online");
//        assignment.setAsmt_frequency(1);
//        assignment.setAsmt_path("C:\\\\packup\\\\ad");
//        assignment.setAsmt_name(my);
//        assignment.setAsmt_ftpname("the ftpname");
//        assignment.setAsmt_ftppwd("the ftppwd");
//        assignment.setAsmt_next("2010-10-8");
//        if(AssignmentManager.insert(assignment) == 0){
//			System.out.println("���ʧ��");
//		}
//       
//        
		Assignment assignment = AssignmentManager.GetLatestAsmt();
		     System.out.println(assignment);
	}
    
}
